package com.yilin.platform.controller.system;

import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.annotation.NotNull;
import com.yilin.platform.base_core.commons.annotation.SkipLoginVerify;
import com.yilin.platform.base_core.commons.annotation.UserTarget;
import com.yilin.platform.base_core.commons.exception.BusinessException;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.entity.enums.user.UserType;
import com.yilin.platform.entity.system.TruckEnergyType;
import com.yilin.platform.entity.system.TruckPlate;
import com.yilin.platform.entity.system.TruckType;
import com.yilin.platform.service.system.IParamService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/param/")
public class ParamController extends BaseController {
    @Resource
    private IParamService paramService;

    /**
     * 获取能源类型
     */
    @PostMapping("getTruckEnergyTypeList")
    public ReturnData<List<TruckEnergyType>> getTruckEnergyTypeList(TruckEnergyType truckEnergyType) {
        List<TruckEnergyType> map = this.paramService.getTruckEnergyTypeList(truckEnergyType);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 根据能源ID获取
     */
    @PostMapping("getTruckEnergyById")
    @Deprecated
    @SkipLoginVerify
    public ReturnData<TruckEnergyType> getTruckEnergyById(String truckEnergyTypeId) {
        TruckEnergyType truckEnergyType = this.paramService.getTruckEnergyById(truckEnergyTypeId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckEnergyType);
    }

    /**
     * 根据车辆ID获取车辆类型
     */
    @PostMapping("getTruckTypeById")
    @Deprecated
    @SkipLoginVerify
    public ReturnData<TruckType> getTruckTypeById(String truckTypeId) {
        TruckType truckType = this.paramService.getTruckTypeById(truckTypeId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckType);
    }

    /**
     * 根据名字获取能源类型
     */
    @PostMapping("getTruckEnergyTypeByName")
    public ReturnData<TruckEnergyType> getTruckEnergyTypeByName(String name) {
        TruckEnergyType truckEnergyTypeByName = this.paramService.getTruckEnergyTypeByName(name);
        return jsonView(SUCCESS_CODE, "获取成功", truckEnergyTypeByName);
    }

    /**
     * 获取车牌类型
     */
    @PostMapping("getTruckPlateList")
    public ReturnData<List<TruckPlate>> getTruckPlateList(TruckPlate truckPlate) {
        List<TruckPlate> map = this.paramService.getTruckPlateList(truckPlate);
        return jsonView(SUCCESS_CODE, "获取列表成功", map);
    }

    /**
     * 根据id获取车牌类型
     */
    @PostMapping("getTruckPlateById")
    public ReturnData<TruckPlate> getTruckPlateById(String truckPlateId) {
        TruckPlate truckPlateById = this.paramService.getTruckPlateById(truckPlateId);
        return jsonView(SUCCESS_CODE, "获取列表成功", truckPlateById);
    }

    /**
     * 获取车辆类型
     */
    @PostMapping("getTreeTruckTypeList")
    public ReturnData<List<TruckType>> getTreeTruckTypeList(TruckType truckType) {
        List<TruckType> list = this.paramService.getTreeTruckTypeList(truckType);
        return jsonView(SUCCESS_CODE, "获取列表成功", list);
    }





    /**
     * 添加/编辑能源类型
     */
    @PostMapping("editTruckEnergyType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckEnergyType(TruckEnergyType truckEnergyType) {
        if (StringUtil.isEmpty(truckEnergyType.getName())) {
            return jsonView(false, "请输入能源类型名称");
        }
        paramService.editTruckEnergyType(truckEnergyType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑车牌
     */
    @PostMapping("editTruckPlate")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckPlate(TruckPlate truckPlate) {
        if (StringUtil.isEmpty(truckPlate.getName())) return jsonView(false, "请输入车牌名称");
        paramService.editTruckPlate(truckPlate);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * 添加/编辑车辆类型
     */
    @PostMapping("editTruckType")
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> editTruckType(TruckType truckType) {
        truckType.setIsTrailer(TruckType.IsTrailer.no);
        if (truckType.getParentId().equals(truckType.getId())) throw new BusinessException("不能选择当前为父节点");
        if (StringUtil.isEmpty(truckType.getName())) return jsonView(false, "请输入车辆类型名称");
        if (truckType.getTowingType() == TruckType.TowingType.trailer) truckType.setIsTrailer(TruckType.IsTrailer.yes);
        paramService.editTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除能源类型
     */
    @PostMapping("deleteTruckEnergyType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckEnergyType(TruckEnergyType truckEnergyType) {
        paramService.deleteTruckEnergyType(truckEnergyType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除车牌
     */
    @PostMapping("deleteTruckPlate")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckPlate(TruckPlate truckPlate) {
        paramService.deleteTruckPlate(truckPlate);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    /**
     * (真实)删除车辆类型
     */
    @PostMapping("deleteTruckType")
    @NotNull({"id"})
    @UserTarget({UserType.adminUser, UserType.platformUser})
    public ReturnData<Object> deleteTruckType(TruckType truckType) {
        if (truckType.getId().equals("0")) throw new BusinessException("根目录不能删除");
        paramService.deleteTruckType(truckType);
        return jsonView(SUCCESS_CODE, "操作成功");
    }




    /**
     * 通过车辆名称检索车辆类型
     */
    @RequestMapping("queryTruckTypeByName")
    public ReturnData<List<TruckType>> queryTruckTypeByName(String name) {
        List<TruckType> list = paramService.queryTruckTypeByName(name);
        return jsonView(true, "获取成功", list);
    }

    /**
     * 通过能源名称检索能源类型
     */
    @PostMapping("queryTruckEnergyTypeByName")
    public ReturnData<TruckEnergyType> queryTruckEnergyTypeByName(String name) {
        TruckEnergyType truckEnergyType = paramService.queryTruckEnergyTypeByName(name);
        return jsonView(true, "获取成功", truckEnergyType);
    }

    /**
     * 通过车牌名称获取车牌类型
     */
    @PostMapping("queryTruckPlateByName")
    public ReturnData<TruckPlate> queryTruckPlateByName(String name) {
        TruckPlate truckPlate = paramService.queryTruckPlateByName(name);
        return jsonView(true, "获取成功", truckPlate);
    }


}
